//
//  YYFoldClock.m
//  翻页动画Demo
//
//  Created by MengXianLiang on 2018/5/28.
//  Copyright © 2018年 jwzt. All rights reserved.
//

#import "YYFoldClock.h"
#import "YYFoldClockItem.h"
#import "UIView+Extra.h"
#import "OCFunc.h"
#import "OCDefine.h"

@interface YYFoldClock () {
    YYFoldClockItem *_hourItem;
    
    YYFoldClockItem *_minuteItem;
    
    YYFoldClockItem *_secondItem;
}
@property(nonatomic,weak)UILabel *hourTopLabel;
@property(nonatomic,weak)UILabel *minuteTopLabel;
@property(nonatomic,weak)UILabel *secondTopLabel;
@end

@implementation YYFoldClock

- (instancetype)init {
    if (self = [super init]) {
        [self buildUI];
    }
    return self;
}

- (void)buildUI {
//    self.backgroundColor =[UIColor blackColor];
    
    _hourItem = [[YYFoldClockItem alloc] init];
    _hourItem.type = XLClockItemTypeHour;
    [self addSubview:_hourItem];
    
    _minuteItem = [[YYFoldClockItem alloc] init];
    _minuteItem.type = XLClockItemTypeMinute;
    [self addSubview:_minuteItem];
    
    _secondItem = [[YYFoldClockItem alloc] init];
    _secondItem.type = XLClockItemTypeSecond;
    [self addSubview:_secondItem];
    
    UILabel *hourLabel=[OCFunc initALabel:@"" textfont:_SYSFONT(17) textColor:[UIColor whiteColor] textAlign:NSTextAlignmentLeft];
    [self addSubview:hourLabel];
    _hourTopLabel=hourLabel;
    
    UILabel *minLabel=[OCFunc initALabel:@"" textfont:_SYSFONT(17) textColor:[UIColor whiteColor] textAlign:NSTextAlignmentLeft];
    [self addSubview:minLabel];
    _minuteTopLabel=minLabel;
    
    UILabel *secLabel=[OCFunc initALabel:@"" textfont:_SYSFONT(17) textColor:[UIColor whiteColor] textAlign:NSTextAlignmentLeft];
    [self addSubview:secLabel];
    _secondTopLabel=secLabel;
    
    self.date = [NSDate date];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    //横屏的时候，此时self.bounds为(origin = (x = 0, y = 0), size = (width = 812, height = 375))
    CGFloat iLeftSpace=0;//(self.width>=812)?80:54; //iphonex下横屏时为60
    CGFloat margin = 0.07*self.bounds.size.width;
    CGFloat itemW = (self.bounds.size.width-2*iLeftSpace -2*margin)/3.0f;
    CGFloat itemY = (self.bounds.size.height - itemW)/2.0f;
    _hourItem.frame = CGRectMake(iLeftSpace, itemY, itemW, itemW);
    _minuteItem.frame = CGRectMake(CGRectGetMaxX(_hourItem.frame) + margin, itemY, itemW, itemW);
    _secondItem.frame = CGRectMake(CGRectGetMaxX(_minuteItem.frame) + margin, itemY, itemW, itemW);
    
//    if (!_font) {
//        self.font=_SYSFONT_BOLD(150);
//        if(self.width<667){
//            self.font=_SYSFONT_BOLD(90);
//        }
//        else if(self.width<812){
//            self.font=_SYSFONT_BOLD(120);
//        }
//    }
//    if (!_titlefont) {
//        self.titlefont=_SYSFONT(19);
//        if(self.width<667){
//            self.titlefont=_SYSFONT(12);
//        }
//        else if(self.width<812){
//            self.titlefont=_SYSFONT(14);
//        }
//    }
    
    _hourTopLabel.frame=CGRectMake(_hourItem.frameX, _hourItem.frameY-30, itemW, 30);
    _minuteTopLabel.frame=CGRectMake(_minuteItem.frameX, _minuteItem.frameY-30, itemW, 30);
    _secondTopLabel.frame=CGRectMake(_secondItem.frameX, _secondItem.frameY-30, itemW, 30);
}

- (void)setDate:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:date];
    _hourItem.time = dateComponent.hour;
    _minuteItem.time = dateComponent.minute;
    _secondItem.time = dateComponent.second;
}

-(void)setCountDown:(NSTimeInterval)countDown{
    NSString *str=[OCFunc formatHourSongTime:countDown];
  
    NSArray *list=[str componentsSeparatedByString:@":"];
    if (list && [list count]>=3) {
        _hourItem.time = [list[0] intValue];
        _minuteItem.time = [list[1] intValue];
        _secondItem.time = [list[2] intValue];
    }
     
    
}

- (void)setFont:(UIFont *)font {
    _hourItem.font = font;
    _minuteItem.font = font;
    _secondItem.font = font;
}

-(void)setTitlefont:(UIFont *)titlefont{
    _hourTopLabel.font=titlefont;
    _minuteTopLabel.font=titlefont;
    _secondTopLabel.font=titlefont;
}

- (void)setTitleColor:(UIColor *)titleColor {
    _hourItem.textColor = titleColor;
    _minuteItem.textColor = titleColor;
    _secondItem.textColor = titleColor;
    _hourTopLabel.textColor=titleColor;
    _minuteTopLabel.textColor=titleColor;
    _secondTopLabel.textColor=titleColor;
}

-(void)setHourTopTitle:(NSString *)hourTopTitle{
    _hourTopTitle=hourTopTitle;
    if (_hourTopLabel) {
        _hourTopLabel.text=hourTopTitle;
    }
}

-(void)setMinuteTopTitle:(NSString *)minuteTopTitle{
    _minuteTopTitle=minuteTopTitle;
    if (_minuteTopLabel) {
        _minuteTopLabel.text=minuteTopTitle;
    }
}

-(void)setSecondTopTitle:(NSString *)secondTopTitle{
    _secondTopTitle=secondTopTitle;
    if (_secondTopLabel) {
        _secondTopLabel.text=secondTopTitle;
    }
}

@end
